package org.jradweb.tools.generator.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A {@link DataSourceFactory} that loads the necessary information from a properties file.
 *
 * @author Tim Ducheyne
 * @author Filip Neven
 */
public class PropertiesDataSourceFactory implements DataSourceFactory {

    /* The logger instance for this class */
    private static Log logger = LogFactory.getLog(PropertiesDataSourceFactory.class);

    /* Propery key of the database driver class name */
    private static final String PROPKEY_DATASOURCE_DRIVERCLASSNAME = "database.driverClassName";

    /* Property key of the datasource url */
    private static final String PROPKEY_DATASOURCE_URL = "database.url";

    /* Property key of the datasource connect username */
    private static final String PROPKEY_DATASOURCE_USERNAME = "database.userName";

    /* Property key of the datasource connect password */
    private static final String PROPKEY_DATASOURCE_PASSWORD = "database.password";

    /* The name of the <code>java.sql.Driver</code> class. */
    private String driverClassName;

    /* The url of the database. */
    private String databaseUrl;

    /* The database username. */
    private String userName;

    /* The database password. */
    private String password;


   
    /**
     * @see DataSourceFactory#createDataSource()
     */
    public Connection getConnection(Properties props) throws SQLException,ClassNotFoundException{
    	Class driverClass = Class.forName(props
				.getProperty(PropertiesLoader.DATABASE_DRIVER_CLASS_NAME));
		Connection jdbcConnection = DriverManager.getConnection(props
				.getProperty(PropertiesLoader.DATABASE_URL), props
				.getProperty(PropertiesLoader.DATABASE_USER_NAME), props
				.getProperty(PropertiesLoader.DATABASE_PASSWORD));
		
		return jdbcConnection;

    }


}
